package com.ruoyi.system.controller.repair;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.db.basic.BasicMaterials;
import com.ruoyi.system.domain.db.repair.MaterialsQuantity;
import com.ruoyi.system.domain.db.repair.MaterialsQuantityName;
import com.ruoyi.system.domain.db.repair.VehicleRepair;
import com.ruoyi.system.domain.dto.MaterialStoreDto;
import com.ruoyi.system.domain.dto.MaterialsQuantityDto;
import com.ruoyi.system.domain.vo.PageInfo;
import com.ruoyi.system.domain.vo.repair.RepairAccendantVo;
import com.ruoyi.system.service.IBasicMaterialsService;
import com.ruoyi.system.service.IVehicleFixService;
import com.ruoyi.system.utils.MyPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/repair/accendant")
public class VehicleFixController extends BaseController {
    @Autowired
    private IVehicleFixService vehicleFixService;

    @Autowired
    private IBasicMaterialsService basicMaterialsService;

    /**
     * 查询报修申请列表
     */
    @PreAuthorize("@ss.hasPermi('repair:accendant:list')")
    @GetMapping("/list")
    public PageInfo<RepairAccendantVo> list(VehicleRepair vehicleRepair)
    {
        // startPage();
        List<RepairAccendantVo> repairDriverVos = vehicleFixService.selectVehicleFixList(vehicleRepair);
       return  MyPageUtils.startPage(repairDriverVos);
    }

    /**
     * 导出报修申请列表
     */
//    @PreAuthorize("@ss.hasPermi('repair:accendant:export')")
//    @Log(title = "维修订单", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, VehicleRepair vehicleRepair)
//    {
//        List<RepairAccendantVo> repairAccendantVos = vehicleFixService.selectVehicleFixList(vehicleRepair);
//        ExcelUtil<RepairAccendantVo> util = new ExcelUtil<RepairAccendantVo>(RepairAccendantVo.class);
//        util.exportExcel(response, repairAccendantVos, "报修申请数据");
//    }


    /**
     * 获取报修申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('repair:accendant:query')")
    @GetMapping(value = "/{repairId}")
    public AjaxResult getInfo(@PathVariable("repairId") Long repairId)
    {
        return success(vehicleFixService.selectVehicleFixByRepairId(repairId));
    }

//    /**
//     * 新增报修申请
//     */
//    @PreAuthorize("@ss.hasPermi('repair:accendant:add')")
//    @Log(title = "报修申请", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody VehicleRepair vehicleRepair)
//    {
//
//        return toAjax(vehicleFixService.insertVehicleRepair(vehicleRepair));
//    }


    /**
     * 修改维修订单（接单）
     */
    @PreAuthorize("@ss.hasPermi('repair:accendant:edit')")
    @Log(title = "维修订单", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody VehicleRepair vehicleRepair)
    {
        return toAjax(vehicleFixService.updateVehicleFix(vehicleRepair));
    }

    /**
     * 上报材料，用材料id
     */
    @PreAuthorize("@ss.hasPermi('repair:accendant:add')")
    @PutMapping("add")
    public AjaxResult add(@RequestBody List<MaterialsQuantity> list){
        List<MaterialsQuantityDto> dtoList = new ArrayList<>();
        for (MaterialsQuantity materialsQuantity : list) {
            MaterialsQuantityDto materialsQuantityDto = materialsQuantityTomaterialsQuantityDto(materialsQuantity); // 转格式
            dtoList.add(materialsQuantityDto);
        }
        return toAjax(vehicleFixService.insertUseMaterialsDuringFix(dtoList));
    }

    /**
     * 上报材料，用材料名字
     */
    @PreAuthorize("@ss.hasPermi('repair:accendant:addByName')")
    @PutMapping("addByName")
    public AjaxResult addByName(@RequestBody List<MaterialsQuantityName> list){
        List<MaterialsQuantityDto> dtoList = new ArrayList<>();
        for (MaterialsQuantityName materialsQuantityName : list) {
            MaterialsQuantityDto materialsQuantityDto = materialsQuantityNameTomaterialsQuantityDto(materialsQuantityName); // 转格式
            dtoList.add(materialsQuantityDto);
        }
        return toAjax(vehicleFixService.insertUseMaterialsDuringFix(dtoList));
    }

    /**
     * 通过材料id获取材料对象（这个用的是主键，不会出问题）
     * @param material_id
     * @return
     */
    private BasicMaterials getBasicMaterials(Long material_id){
        return basicMaterialsService.selectBasicMaterialsByMaterialId(material_id);
    }

    /**
     * 通过材料名获取材料对象（当出现材料名重复时，会有问题）
     * @param material_name
     * @return
     */
    private BasicMaterials getBasicMaterialsByName(String material_name){
        List<BasicMaterials> basicMaterials = basicMaterialsService.selectBasicMaterialsList(new BasicMaterials());
        for (BasicMaterials basicMaterial : basicMaterials) {
            if (basicMaterial.getMaterialName().equals(material_name)){
                return basicMaterial;
            }
        }
        return null; // 找不到
    }

    /**
     * 转格式
     * @param materialsQuantity
     * @return
     */
    private MaterialsQuantityDto materialsQuantityTomaterialsQuantityDto(MaterialsQuantity materialsQuantity){
        MaterialsQuantityDto materialsQuantityDto = new MaterialsQuantityDto();
        materialsQuantityDto.setRepair_id(materialsQuantity.getRepair_id());
        materialsQuantityDto.setMaterials(getBasicMaterials(materialsQuantity.getMaterialId()));
        materialsQuantityDto.setQuantity(materialsQuantity.getQuantity());
        return materialsQuantityDto;
    }

    /**
     * 转格式
     * @param materialsQuantityName
     * @return
     */
    private MaterialsQuantityDto materialsQuantityNameTomaterialsQuantityDto(MaterialsQuantityName materialsQuantityName){
        MaterialsQuantityDto materialsQuantityDto = new MaterialsQuantityDto();
        materialsQuantityDto.setRepair_id(materialsQuantityName.getRepair_id());
        materialsQuantityDto.setMaterials(getBasicMaterialsByName(materialsQuantityName.getMaterialName()));
        materialsQuantityDto.setQuantity(materialsQuantityName.getQuantity());
        return materialsQuantityDto;
    }


    /**
     * 获取所有剩余材料
     */
    @PreAuthorize("@ss.hasPermi('repair:accendant:getStoreMaterial')")
    @GetMapping("/getStoreMaterial")
    public TableDataInfo getStoreMaterials(){
        List<BasicMaterials> materialList = vehicleFixService.selectAllMaterials();
        List<MaterialStoreDto> materialStoreDtos = new ArrayList<>();
        for (BasicMaterials materials : materialList) {
            MaterialStoreDto materialStoreDto = vehicleFixService.selectAllMaterialStore(materials).get(0);
            materialStoreDtos.add(materialStoreDto);
        }
        return getDataTable(materialStoreDtos);
    }

//    /**
//     * 获取所有材料
//     */
//    @PreAuthorize("@ss.hasPermi('repair:accendant:getallmaterials')")
//    @GetMapping("/getAllMaterials")
//    public TableDataInfo getAllMaterials(){
//        return getDataTable(vehicleFixService.selectAllMaterials());
//    }

//    /**
//     * 删除维修订单
//     */
//    @PreAuthorize("@ss.hasPermi('repair:accendant:remove')")
//    @Log(title = "维修订单", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{repairIds}")
//    public AjaxResult remove(@PathVariable Long[] repairIds)
//    {
//        return toAjax(vehicleFixService.deleteVehicleRepairByRepairIds(repairIds));
//    }
}
